<?php
/**
 * Created by PhpStorm.
 * User: xuechunxuan
 * Date: 2018/10/17
 * Time: 11:44
 */
namespace app\index\controller;

class Base extends \think\Controller{
   public  function http_request($curl , $https=true , $method="get" , $data=null){
        $ch = curl_init();   //初始化
        curl_setopt($ch, CURLOPT_URL, $curl);//设置访问的url
        curl_setopt($ch, CURLOPT_HEADER, false);//设置不需要头信息
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//只去获取页面内容，打不输出

        if($https){
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//不做服务器的认证
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//不做客户端认证
        }

        if($method == 'post'){
            curl_setopt($ch, CURLOPT_POST, true);//设置请求是post方式
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//设置post请求的数据
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
        }

        if($method == 'put'){
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");//设置请求是put方式
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//设置put请求的数据
        }

        $str = curl_exec($ch);//执行访问
        $httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
        if($httpCode < 200 || $httpCode >= 300){
            exception($str,$httpCode);
        }
        curl_close($ch);//执行访问，关闭资源
        return $str;
    }


}